package com.ymh.shiro.web.realm;

import com.ymh.shiro.web.dao.UserDao;
import com.ymh.shiro.web.vo.User;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by aivin on 2018/8/19.
 * 自定义的Realm
 */
public class CustomRealm extends AuthorizingRealm {


    @Autowired
    private UserDao userDao;

    /**
     * 授权方法
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        String userName = (String) principalCollection.getPrimaryPrincipal();
        //从数据库或者缓存中获取角色数据
        Set<String> roles = getRolesByUserName(userName);
        //从数据库或者缓存中获取权限信息
//        Set<String> permissions = getPermissionsByUserName(userName);

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.setRoles(roles);
//        simpleAuthorizationInfo.setStringPermissions(permissions);

        return simpleAuthorizationInfo;
    }

    /**
     * 模拟从数据库或者缓存中获取权限信息
     *
     * @param userName
     * @return
     */
    private Set<String> getPermissionsByUserName(String userName) {
        List<String> list = userDao.getPermissionByUserName(userName);
        Set<String> sets = new HashSet<>(list);
        return sets;
    }

    /**
     * 模拟从数据库或者缓存中获取角色数据
     *
     * @param userName
     * @return
     */
    private Set<String> getRolesByUserName(String userName) {
        List<String> list = userDao.getRolesByUserName(userName);
        Set<String> sets = new HashSet<>(list);
        return sets;
    }

    /**
     * 认证方法
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //1、从主体传过来的认证信息中，获得用户名
        String username = (String) authenticationToken.getPrincipal();

        //2、通过用户名到数据库获取凭证
        String password = getPasswordByUserName(username);
        if (password == null) {
            return null;
        }
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(username, password, "customRealm");
        simpleAuthenticationInfo.setCredentialsSalt(ByteSource.Util.bytes(username)); //加盐
        return simpleAuthenticationInfo;
    }

    /**
     * 模拟数据库查询凭证
     *
     * @param username
     * @return
     */
    private String getPasswordByUserName(String username) {
        User user = userDao.getUserByUserName(username);
        if (user != null) {
            System.out.println("username=" + user.getUsername() + " \npassword=" + user.getPassword());
            return user.getPassword();
        }
        return null;
    }


    public static void main(String[] args) {
//        Md5Hash md5Hash = new Md5Hash("123456"); //e10adc3949ba59abbe56e057f20f883e
        Md5Hash md5Hash = new Md5Hash("123456", "Mark"); //加盐
        System.out.println(md5Hash);
    }
}
